from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer

from authentication.utils import get_user

oauth2_scheme = OAuth2PasswordBearer(
    tokenUrl="/api/authentication/token"
)


def sha1_hash_pwd(password: str):
    import hashlib
    encrypted_pwd = hashlib.sha1(password.encode('utf-8'))
    # save binary into db with 'encrypted.digest()'
    return encrypted_pwd.hexdigest()


def fake_decode_token(token):
    # This doesn't provide any security at all
    # Check the next version
    user = get_user(token)
    return user


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid authentication credentials",
            headers={"WWW-Authenticate": "Bearer"},
        )
    return user


# async def get_current_active_user(current_user: UserInDB = Depends(get_current_user)):
#     if current_user.disabled:
#         raise HTTPException(status_code=400, detail="Inactive user")
#     ret = current_user.dict()
#     ret.pop('hashed_password')
#     return User(**ret)
